<template>
	<el-dialog v-model="dialog" :title="state.form.id ? '修改部门' : '添加部门'" width="60%" :before-close="handleClose">
		<el-form ref="formRef" :model="state.form" label-width="120px" :rules="state.rules">
			<el-form-item label="部门名称" prop="deptName">
				<el-input v-model="state.form.deptName"></el-input>
			</el-form-item>
			<el-form-item label="上级部门" prop="parentId" v-if="state.form.parentId != '0'">
				<el-tree-select
					style="width: 100%"
					v-model="state.form.parentId"
					:data="state.deptTree"
					check-strictly
					:props="defaultProps"
					:render-after-expand="false"
					node-key="id"
				></el-tree-select>
			</el-form-item>
			<el-form-item label="负责人" prop="leader">
				<el-input v-model="state.form.leader"></el-input>
			</el-form-item>
			<el-form-item label="手机号" prop="leaderPhone">
				<el-input v-model="state.form.leaderPhone"></el-input>
			</el-form-item>
			<el-form-item label="排序序号" prop="sort">
				<el-input-number v-model="state.form.sort" :min="0"></el-input-number>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-radio-group v-model="state.form.status">
					<el-radio v-for="item in status" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
				</el-radio-group>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleClose">关 闭</el-button>
				<el-button type="primary" :loading="loading" @click="submitForm(formRef)">确 认</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { getTreeList, addObj, editObj, getById } from '/@/api/upms/sysdept';
import { ElMessage, FormInstance, FormRules } from 'element-plus';
import { useDict } from '/@/utils/dict';
// 字典
const { status } = useDict('status');
const emit = defineEmits(['initPage']);
const state = reactive({
	form: { id: undefined, deptName: '', parentId: '', leader: '', leaderPhone: '', sort: 0, status: '0' },
	rules: {
		deptName: [
			{
				required: true,
				message: '请输入部门名称',
				trigger: 'change',
			},
		],
		parentId: [
			{
				required: true,
				message: '请选择上级部门',
				trigger: 'change',
			},
		],
		leader: [
			{
				required: true,
				message: '请输入负责人',
				trigger: 'change',
			},
		],
		sort: [
			{
				required: true,
				message: '请输入排序序号',
				trigger: 'change',
			},
		],
		status: [
			{
				required: true,
				message: '请选择状态',
				trigger: 'change',
			},
		],
	},
	deptTree: [],
});
const dialog = ref(false);
const loading = ref(false);
const formRef = ref();
const defaultProps = {
	children: 'children',
	label: 'name',
};
const initForm = (row: any) => {
	dialog.value = true;
	if (row && row.id) {
		getDetail(row.id);
	}
	getDeptData();
};
const getDetail = (id: string) => {
	loading.value = true;
	// 修改
	getById(id)
		.then((response: any) => {
			loading.value = false;
			state.form = response.data.data;
		})
		.catch(() => {
			loading.value = false;
		});
};
/**
 * 关闭事件
 */
const handleClose = () => {
	resetForm(formRef.value);
};
/**
 * 重置表单
 */
const resetForm = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	state.form.id = undefined;
	loading.value = false;
	dialog.value = false;
	formEl.resetFields();
};
/**
 * 提交按钮
 */
const submitForm = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid, fields) => {
		if (valid) {
			loading.value = true;
			if (state.form.id) {
				//修改
				edit();
			} else {
				//新增
				add();
			}
		}
	});
};
/**
 * 新增
 */
const add = () => {
	addObj(state.form)
		.then(() => {
			resetForm(formRef.value);
			ElMessage.success('新增成功');
			emit('initPage');
		})
		.catch(() => {
			loading.value = false;
		});
};
/**
 * 修改
 */
const edit = () => {
	editObj(state.form)
		.then(() => {
			resetForm(formRef.value);
			ElMessage.success('修改成功');
			emit('initPage');
		})
		.catch(() => {
			loading.value = false;
		});
};
const getDeptData = async () => {
	await getTreeList().then((response: any) => {
		state.deptTree = response.data.data;
	});
};
defineExpose({
	initForm,
});
</script>
